package com.edu.blog.service.impl;

import com.edu.blog.entity.SysUser;
import com.edu.blog.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CustomUserService implements UserDetailsService { //自定义UserDetailsService 接口

    @Autowired
    private UserMapper userMapper;

    public UserDetails loadUserByUsername(String username) {
        SysUser user=userMapper.findUserByName1(username);
        if(user!=null){
            List<GrantedAuthority> grantedAuthorities=new ArrayList<>();
            return new User(user.getUsername(),user.getPassword(),grantedAuthorities);
        }else{
            throw new UsernameNotFoundException("admin:"+username+"不存在");
        }
    }

}
